package com.woniuxy.third.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.woniuxy.third.result.ResponseResult;
import com.woniuxy.third.service.VideoService;
import com.woniuxy.third.video.InitAliyunClient;
import com.woniuxy.third.video.VodConstants;
import io.swagger.annotations.ApiModelProperty;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/video")
@CrossOrigin
public class VideoController {
    @Resource
    private  VideoService videoService;

    @PostMapping("/uploadAliyunVideo")
    public ResponseResult<String> uploadAliyunVideo(MultipartFile file) throws IOException {

        String videoId = videoService.uploadVideoToAliyun(file);
        return new ResponseResult<String>();
    }

    @DeleteMapping("/removeAliyunVideo")
    public ResponseResult<Integer> removeAliyunVideo(String id) throws ClientException {
        //初始化阿里云客户端对象
        DefaultAcsClient client = InitAliyunClient.initVodClient(VodConstants.ACCESS_KEY_ID, VodConstants.ACCESS_KEY_SECRET);

        //创建删除视频的request对象
        DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
        deleteVideoRequest.setVideoIds(id);
        try {
            client.getAcsResponse(deleteVideoRequest);
        } catch (ClientException e) {
            e.printStackTrace();

        }
        ResponseResult<Integer> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setData(1);
        return responseResult;
    }

    @DeleteMapping("/deleteBatch")
    public ResponseResult deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        videoService.deleteBatch(videoIdList);
        return new ResponseResult().setCode(200);
    }

    @RequestMapping("/getPlayAuth")
    public ResponseResult<String> getPlayAuth( String id ) throws ClientException {

        //创建初始化对象
        DefaultAcsClient client = InitAliyunClient.initVodClient(VodConstants.ACCESS_KEY_ID, VodConstants.ACCESS_KEY_SECRET);

        //创建获取凭证的request和response对象
        GetVideoPlayAuthRequest authRequest = new GetVideoPlayAuthRequest();

        //向authRequest设置视频id
        authRequest.setVideoId(id);

        //调用方法得到凭证
        GetVideoPlayAuthResponse acsResponse = null;
        try {
            acsResponse = client.getAcsResponse(authRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        String playAuth = acsResponse.getPlayAuth();

        return new ResponseResult<String>().setCode(200).setData(playAuth);
    }

}
